##########################################################
# Código para extrair os gráficos a partir da razão entre
# probabilidades preditas.
#
# Autoria: Leonardo Rodrigues
# Código escrito e executado no RStudio (Version 1.3.1073)
##########################################################

### 1 Preparação ----
# Leitura dos pacotes
library(ggplot2)
library(foreign)
library(DescTools)
library(dplyr)
library(stringr)
library(forcats)
library(ggpubr)
library(gt)
library(reshape2)

### 2 Gráficos a partir do modelo multinomial ----

pps <- read.table("data/output/ratioppsareas.txt", sep="\t")

#Grafico da variavel raça/cor
BN <-  pps[, c("area","cor1", "cor2")]
BN <- BN[order(BN$cor2), ]                                    
BN$area <- factor(BN$area, levels = BN$area)
BN <- melt(BN, id = "area")

raça <- ggplot(BN, aes(y = value, x = area)) +
  geom_point(aes(color=factor(variable, labels = c("Ciclo 1", "Ciclo 2")),
                 shape=factor(variable, labels = c("Ciclo 1", "Ciclo 2"))), size = 4.0) +
  scale_shape_manual(values=c(15, 19)) +
  scale_color_manual(values=c("red2","green4")) +
  scale_y_continuous(limits=c(1,2), breaks=seq(1, 2, 0.1)) +
  geom_hline(yintercept = c(-1.0,1.0), linetype = "dotted", size = 1) +
  labs(y = "Brancos/Negros", color = "Ciclo", shape = "Ciclo") +
  coord_flip() +
  theme_bw()

raça + theme(axis.title.x = element_text(family = "serif", face = "bold", size = 10))


raça1 <- ggplot(BN, aes(y = value, x = area)) +
  geom_point(aes(color=factor(variable, labels = c("Ciclo 1", "Ciclo 2")),
                 shape=factor(variable, labels = c("Ciclo 1", "Ciclo 2"))), size = 4.0) +
  scale_shape_manual(values=c(15, 19)) +
  scale_color_manual(values=c("red2","green4")) +
  scale_y_continuous(limits=c(-2,-1), breaks=seq(-2, -1, 0.1)) +
  geom_hline(yintercept = c(-1.0,1.0), linetype = "dotted", size = 1) +
  labs(y = "Negros/Brancos", x = "none") +
  coord_flip() + theme_bw() +
  theme(axis.text.y = element_text(size = 10),
        legend.position="none", axis.title.y = element_blank(), legend.title = element_blank())

graficoraça <- ggarrange(raça1, 
          raça + theme(axis.text.y = element_blank(),
                       axis.ticks.y = element_blank(),
                       axis.title.y = element_blank()), nrow = 1)

graficoraça
png(filename = "data/output/raca.png", type = "cairo", units =
      "in", width = 9, height = 6, pointsize = 16, res = 400)
graficoraça
dev.off()

#Grafico da variável de escolaridade dos pais
SM <-  pps[, c("area", "edu1", "edu2")]
SM <- SM[order(SM$edu2), ]
SM$area <- factor(SM$area, levels = SM$area)
SM <- melt(SM, id = "area")

escola <- ggplot(SM, aes(y = value, x = area)) +
  geom_point(aes(color=factor(variable, labels = c("Ciclo 1", "Ciclo 2")),
                 shape=factor(variable, labels = c("Ciclo 1", "Ciclo 2"))), size = 4.0) +
  scale_shape_manual(values=c(15, 19)) +
  scale_color_manual(values=c("red2","green4")) +
  scale_y_continuous(limits=c(1,8), breaks=seq(1, 8, 1)) +
  geom_hline(yintercept = c(-1.0,1.0), linetype = "dotted", size = 1) +
  labs(y = "Alta/Baixa", color = "Ciclo", shape = "Ciclo") +
  coord_flip() + theme_bw()


escola + theme(axis.title.x = element_text(family = "serif", face = "bold", size = 10))


escola1 <- ggplot(SM, aes(y = value, x = area)) +
  geom_point(aes(color=factor(variable, labels = c("Ciclo 1", "Ciclo 2")),
                 shape=factor(variable, labels = c("Ciclo 1", "Ciclo 2"))), size = 4.0) +
  scale_shape_manual(values=c(15, 19)) +
  scale_color_manual(values=c("red2","green4")) +
  scale_y_continuous(limits=c(-8,-1), breaks=seq(-8, -1, 1)) +
  geom_hline(yintercept = c(-1.0,1.0), linetype = "dotted", size = 1) +
  labs(y = "Baixa/Alta", x = "none") +
  coord_flip() + theme_bw() +
  theme(axis.text.y = element_text(size = 10),
        legend.position="none", axis.title.y = element_blank(), legend.title = element_blank())

graficoescola <- ggarrange(escola1, 
          escola + theme(axis.text.y = element_blank(),
                         axis.ticks.y = element_blank(),
                         axis.title.y = element_blank()), nrow = 1)

graficoescola
png(filename = "data/output/escolaridade.png", type = "cairo", units =
      "in", width = 9, height = 6, pointsize = 16, res = 400)
graficoescola
dev.off()

#Gráfico para a variável de sexo
MF <-  pps[, c("area", "sexo1", "sexo2")]
MF <- MF[order(MF$sexo2), ]
MF$area <- factor(MF$area, levels = MF$area)
MF <- melt(MF, id = "area")

sexo <- ggplot(MF, aes(y = value, x = area)) +
  geom_point(aes(color=factor(variable, labels = c("Ciclo 1", "Ciclo 2")),
                 shape=factor(variable, labels = c("Ciclo 1", "Ciclo 2"))), size = 4.0) +
  scale_shape_manual(values=c(15, 19)) +
  scale_color_manual(values=c("red2","green4")) +
  scale_y_continuous(limits=c(1,5), breaks=seq(1, 5, 0.5)) +
  geom_hline(yintercept = c(-1.0,1.0), linetype = "dotted", size = 1) +
  labs(y = "Masculino/Feminino", color = "Ciclo", shape = "Ciclo") +
  coord_flip() + theme_bw()


sexo + theme(axis.title.x = element_text(family = "serif", face = "bold", size = 10))


sexo1 <- ggplot(MF, aes(y = value, x = area)) +
  geom_point(aes(color=factor(variable, labels = c("Ciclo 1", "Ciclo 2")),
                 shape=factor(variable, labels = c("Ciclo 1", "Ciclo 2"))), size = 4.0) +
  scale_shape_manual(values=c(15, 19)) +
  scale_color_manual(values=c("red2","green4")) +
  scale_y_continuous(limits=c(-5,-1), breaks=seq(-5, -1, 0.5)) +
  geom_hline(yintercept = c(-1.0,1.0), linetype = "dotted", size = 1) +
  labs(y = "Feminino/Masculino", x = "none") +
  coord_flip() + theme_bw() +
  theme(axis.text.y = element_text(size = 10),
        legend.position="none", axis.title.y = element_blank(), legend.title = element_blank())

graficosexo <- ggarrange(sexo1, 
          sexo + theme(axis.text.y = element_blank(),
                       axis.ticks.y = element_blank(),
                       axis.title.y = element_blank()), nrow = 1)

graficosexo
png(filename = "data/output/sexo.png", type = "cairo", units =
      "in", width = 9, height = 6, pointsize = 16, res = 400)
graficosexo
dev.off()


### 3 Gráficos a partir do modelo logit ----

pps <- read.table("data/output/ppslogit.txt", sep="\t")

MF <- pps[c(1,2,5)]
BN <- pps[c(1, 4, 7)]
SM <- pps[c(1,3,6)]

BN <- melt(BN, id = "area")
SM <- melt(SM, id = "area")
MF <- melt(MF, id = "area")

BN <- BN[order(BN$value), ]
SM <- SM[order(SM$value), ]
MF <- MF[order(MF$value), ]

BN <- BN %>% mutate(area = fct_reorder(area, value))
SM <- SM %>% mutate(area = fct_reorder(area, value))
MF <- MF %>% mutate(area = fct_reorder(area, value))

levels(BN$variable)[levels(BN$variable)=="BN2"] <- "2"
levels(BN$variable)[levels(BN$variable)=="BN1"] <- "1"
levels(SM$variable)[levels(SM$variable)=="SM2"] <- "2"
levels(SM$variable)[levels(SM$variable)=="SM1"] <- "1"
levels(MF$variable)[levels(MF$variable)=="MF2"] <- "2"
levels(MF$variable)[levels(MF$variable)=="MF1"] <- "1"

#para a variável Raça/cor
raça <- ggplot(BN, aes(y = value, x = area)) +
  geom_point(aes(color=variable, shape=variable), size = 4.0) +
  scale_shape_manual(values=c(15, 19)) +
  scale_color_manual(values=c("red2","green4")) +
  scale_y_continuous(limits=c(0.7,1.3), breaks=seq(0.7, 1.3, 0.05)) +
  geom_hline(yintercept = c(1.0), linetype = "dotted", size = 1) +
  labs(y = "Probabilidades preditas de brancos/negros no setor privado") +
  coord_flip() + labs(color = "ciclo", shape = "ciclo") +
  scale_x_discrete(labels = c("Bacharelados*^", "Direito", "Engenharia^",
                              "Tecnólogos*^", "Licenciatura*^", "Medicina*^")) + theme_bw() +
  theme(axis.title.y=element_blank(), axis.text.y = element_text(size = 10))

raça
png(filename = "data/output/racalogit.png", type = "cairo", units =
      "in", width = 9, height = 6, pointsize = 16, res = 400)
raça
dev.off()

#para a variável Sexo
sexo <- ggplot(MF, aes(y = value, x = area)) +
  geom_point(aes(color=variable, shape=variable), size = 4.0) +
  scale_shape_manual(values=c(15, 19)) +
  scale_color_manual(values=c("red2","green4")) +
  scale_y_continuous(limits=c(0.7,1.3), breaks=seq(0.7, 1.3, 0.05)) +
  geom_hline(yintercept = c(1.0), linetype = "dotted", size = 1) +
  labs(y = "Probabilidade predita de homens/mulheres no setor privado") +
  coord_flip() + labs(color = "ciclo", shape = "ciclo") +
  scale_x_discrete(labels = c("Licenciatura*^", "Tecnólogos*^", "Medicina*",
                              "Bacharelados*^", "Direito*", "Engenharia*^")) + theme_bw() +
  theme(axis.title.y=element_blank(), axis.text.y = element_text(size = 10))

sexo
png(filename = "data/output/sexologit.png", type = "cairo", units =
      "in", width = 9, height = 6, pointsize = 16, res = 400)
sexo
dev.off()


#para a variável escolaridade dos pais
escola <-  SM %>%
  ggplot(aes(y = value, x = area)) +
  geom_point(aes(color=variable, shape=variable), size = 4.0) +
  scale_shape_manual(values=c(15, 19)) +
  scale_color_manual(values=c("red2","green4")) +
  scale_y_continuous(limits=c(0.7,1.3), breaks=seq(0.7, 1.3, 0.05)) +
  geom_hline(yintercept = c(1.0), linetype = "dotted", size = 1) +
  labs(y = "Probabilidade predita de filhos de pais com ensino superior/ensino médio no setor privado") +
  coord_flip() + labs(color = "ciclo", shape = "ciclo") +
  scale_x_discrete(labels = c("Engenharia*^", "Bacharelados*^", "Licenciaturas*",
                              "Direito*^", "Tecnólogos*", "Medicina*^")) + theme_bw() +
  theme(axis.title.y=element_blank(), axis.text.y = element_text(size = 10))

escola
png(filename = "data/output/escolalogit.png", type = "cairo", units =
      "in", width = 9, height = 6, pointsize = 16, res = 400)
escola
dev.off()
